<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/HtmlElement.php';

/**
 * Helper to generate a cached "image" element
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3image extends Zend_View_Helper_HtmlElement
{

	/**
	 * Generates a cached image
	 *
	 * @param string $path
	 * @param integer $width
	 * @param integer $height
	 * @param string $type
	 * @param array $attribs
	 * @return string The element XHTML.
	 */
    public function t3image($path, $width, $height, $type='jpg', array $attribs = array())
    {
		$imgObj = t3lib_div::makeInstance('t3lib_stdGraphic');
		$imgObj->init();
		$imgObj->mayScaleUp = 0;
    	$xhtml = '';
		$imgInfo = $imgObj->getImageDimensions($path);
    	if(is_array($imgInfo)) {
			$imgInfo_scaled = $imgObj->imageMagickConvert($path, $type, $width, $height, '', '', '', 1);
			if(is_array($imgInfo_scaled)) {
				$xhtml = $this->imgTag($imgInfo_scaled, $attribs);
			} else {
				return '<div>Error: ImageMagick is not installed.</div>';
			}
		}
		return $xhtml;
	}

	/**
	 * Genereates the image html tag
	 *
	 * @param array $imgInfo
	 * @param array $attribs
	 * @return string The element XHTML.
	 */
	public function imgTag(array $imgInfo, array $attribs = array())
	{

		if(!isset($attribs['alt'])) {
			$attribs['alt'] = '';
		}

		$attribs['width']	= $imgInfo[0];
		$attribs['height']	= $imgInfo[1];
		$attribs['src']		= $imgInfo[3];

		return '<img'
				. $this->_htmlAttribs($attribs)
				. ' />';
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3image.php']) {
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3image.php']);
}
